package com.example.ymm.modules1.controller.ms;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.hl.dkn.DknStockProduct;
import com.example.ymm.modules1.entity.ms.*;
import com.example.ymm.modules1.entity.my.ms.PmcMaterialRef;
import com.example.ymm.modules1.entity.my.ms.PmcNote;
import com.example.ymm.modules1.query.hl.DknStockProductQueryCriteria;
import com.example.ymm.modules1.query.ms.*;
import com.example.ymm.modules1.service.dto.heng.ImportStockProductDto;
import com.example.ymm.modules1.service.dto.ms.ImportMaterialRefDto;
import com.example.ymm.modules1.service.dto.ms.MsWarehouseDto;
import com.example.ymm.modules1.service.dto.ms.PmcMaterialRefQueryCriteria;
import com.example.ymm.modules1.service.hl.DknStockProductService;
import com.example.ymm.modules1.service.ms.*;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hl/sys/ms/stock")
@Api(tags = "ms 仓储管理")
@Slf4j
public class MsStockController extends BaseController {

    @Autowired
    MsOrderItemViewService msOrderItemViewService;
    @Autowired
    VckStockService vckStockService;

    @Autowired
    DknStockProductService dknStockProductService;
    @Autowired
    private PmcMaterialRefService pmcMaterialRefService;
    @Autowired
    PmcNoteService pmcNoteService;
    @Autowired
    PmcOrderService pmcOrderService;


    @ApiOperation(value = "迪卡侬配件仓 库位")
    @RequestMapping(value = "/dknWarehouseList", method = RequestMethod.POST)
    public CommonResult dknWarehouseList(MsWarehouseDtoQueryCriteria criteria) {
        try {
            criteria.setWiname("DKL配件仓");
//            criteria.setWinameLikeBefore("包材区");
//            if(UtilValidate.isEmpty(criteria.getWinameLikeBefore())){
//                if(UtilValidate.isNotEmpty(criteria.getIsJiaZi())){
//                    criteria.setWinameLikeBefore("包材区（架子）");
//                }else{
//                    criteria.setWinameLikeBefore("包材区");
//                    criteria.setWinameNoLike("（架子）");
//                }
//            }
            List<MsWarehouseDto> list = vckStockService.warehouseList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "铁架区 库位")
    @RequestMapping(value = "/tieJiaList", method = RequestMethod.POST)
    public CommonResult tieJiaList(MsWarehouseDtoQueryCriteria criteria) {
        try {
//            criteria.setWiname("配件");
            List<String> winameIn=new ArrayList<>();
            winameIn.add("OEM石英配件仓");
            winameIn.add("时俱佳材料仓");
            criteria.setWinameIn(winameIn);
            if(UtilValidate.isNotEmpty(criteria.getIsJiaZi())){


                //库位右区
                criteria.setWinameLikeBefore("配件铁架A");
            }else{
                //库位左区
                criteria.setWinameLikeBefore("配件");
                criteria.setWinameNoLike("铁架A");
            }
            List<MsWarehouseDto> list = vckStockService.warehouseList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "三楼铁架仓库存列表-无预警")
    @RequestMapping(value = "/tiejiaStockList", method = RequestMethod.POST)
    public CommonResult tiejiaStockList(VckStockQueryCriteria criteria, YmPageDto ymPage) {
        try {
            List<String> winameIn=new ArrayList<>();
            winameIn.add("OEM石英配件仓");
            winameIn.add("时俱佳材料仓");
            criteria.setWinameIn(winameIn);

            TableList<VckStock> list = vckStockService.listNoRef(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "迪卡侬配件仓库存列表")
    @RequestMapping(value = "/stockList", method = RequestMethod.POST)
    public CommonResult stockList(VckStockQueryCriteria criteria, YmPageDto ymPage) {
        try {
            criteria.setWiname("DKL配件仓");
//            criteria.setKuWeiIsNotNull("1");
            if(UtilValidate.isEmpty(criteria.getKuWeiLikeBefore())){
                criteria.setKuWeiLikeBefore("配件2区");
//                List<String> nameLikeIn=new ArrayList<>();
//                nameLikeIn.add("机芯区");
//                nameLikeIn.add("配件1区");
//                criteria.setKuWeiNameLikeIn(nameLikeIn);
            }

//            if(UtilValidate.isEmpty(criteria.getKuWeiTid())){
//                if(UtilValidate.isNotEmpty(criteria.getIsJiaZi())){
//                    criteria.setKuWeiLikeBefore("包材区（架子）");
//                }else{
//                    criteria.setKuWeiLikeBefore("包材区");
//                    criteria.setKuWeiNoLike("（架子）");
//                }
//            }
            TableList<VckStock> list = vckStockService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "迪卡侬配件仓库存列表-无预警")
    @RequestMapping(value = "/stockNoRefList", method = RequestMethod.POST)
    public CommonResult stockNoRefList(VckStockQueryCriteria criteria, YmPageDto ymPage) {
        try {
            if(UtilValidate.isEmpty(criteria.getWiname())){
                criteria.setWiname("DKL配件仓");
            }

            if(UtilValidate.isEmpty(criteria.getKuWeiLikeBefore())){
//                criteria.setKuWeiLikeBefore("包材区");
//                criteria.setQueryBaoJiXin("1");// 包材区 和 机心区
                List<String> nameLikeIn=new ArrayList<>();
                nameLikeIn.add("机心区");
                nameLikeIn.add("配件1区");
                criteria.setKuWeiNameLikeIn(nameLikeIn);
            }

            TableList<VckStock> list = vckStockService.listNoRef(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "迪卡侬配件仓库存列表-语音")
    @RequestMapping(value = "/stockMiniList", method = RequestMethod.POST)
    public CommonResult stockMiniList(VckStockQueryCriteria criteria, YmPageDto ymPage) {
        try {
            if(UtilValidate.isEmpty(criteria.getWiname())){
                criteria.setWiname("DKL配件仓");
            }

            if(UtilValidate.isEmpty(criteria.getKuWeiLikeBefore())){
//                criteria.setKuWeiLikeBefore("包材区");
//                criteria.setQueryBaoJiXin("1");// 包材区 和 机芯区
                List<String> nameLikeIn=new ArrayList<>();
                nameLikeIn.add("机芯区");
                nameLikeIn.add("配件1区");
                criteria.setKuWeiNameLikeIn(nameLikeIn);
            }

            TableList<VckStock> list = vckStockService.listNoRef(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    //迪卡侬配件仓亮灯
    @ApiOperation(value = "迪卡侬配件仓亮灯")
    @RequestMapping(value = "/stockPcLight", method = RequestMethod.POST)
    public CommonResult stockPcLight(@RequestBody List<VckStock> stocks) {
        try {
            if(UtilValidate.isEmpty(stocks)){
                return CommonResult.error("参数不能为空");
            }
            msOrderItemViewService.stockPcLight(stocks);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "通用仓库存列表-无预警")
    @RequestMapping(value = "/stockCommonList", method = RequestMethod.POST)
    public CommonResult stockCommonList(@RequestBody VckStockQueryCriteria criteria) {
        try {
            if(UtilValidate.isEmpty(criteria.getPage())){
                return CommonResult.error("参数有误");
            }
            YmPageDto ymPage=new YmPageDto();
            ymPage.setPage(criteria.getPage());
            ymPage.setPageSize(criteria.getPageSize());
            TableList<VckStock> list = vckStockService.listNoRef(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "库位 列表通用")
    @RequestMapping(value = "/kuweiCommonList", method = RequestMethod.POST)
    public CommonResult kuweiCommonList(@RequestBody MsWarehouseDtoQueryCriteria criteria) {
        try {
//            criteria.setWiname("配件");
            if(UtilValidate.isEmpty(criteria)){
                return CommonResult.error("参数有误");
            }
//            if(UtilValidate.isEmpty(criteria.getWinameLikeBefore())){
//                return CommonResult.error("库位参数不能为空");
//            }

            List<MsWarehouseDto> list = vckStockService.warehouseList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "库容产品列表")
    @PostMapping("/stockProductList")
    public CommonResult stockProductList(@Valid DknStockProductQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<DknStockProduct> list = dknStockProductService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "库位产品保存")
    @PostMapping("/stockProductSave")
    public CommonResult stockProductSave(DknStockProduct product) {
        try {
            dknStockProductService.saveOrUpdate(product);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "库位产品删除")
    @PostMapping("/stockProductRemove")
    public CommonResult stockProductRemove(DknStockProduct productMapping) {
        try {
            dknStockProductService.removeById(productMapping);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "库位产品导入")
    @PostMapping("/importStockProduct")
    public CommonResult importStockProduct(@RequestBody ImportStockProductDto dto) {
        try {
            List<String> strings = dknStockProductService.importStockProduct(dto);
            return CommonResult.success(strings);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    /**
     * 销售发货出库管理
     */
    @ApiOperation(value = "销售发货出库列表")
    @PostMapping("/outbillList")
    public CommonResult outbillList(@Valid CkOutbillQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<CkOutbillList> list = vckStockService.outbillPageList(criteria,ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "销售发货出库列表 all")
    @PostMapping("/outbillAllList")
    public CommonResult outbillAllList(@Valid CkOutbillQueryCriteria criteria) {
        try {
            List<CkOutbillList> list = vckStockService.outbillList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "销售发货出库列表 暂存")
    @PostMapping("/outbillStorageList")
    public CommonResult outbillStorageList(@Valid CkOutbillQueryCriteria criteria) {
        try {

            criteria.setQueryStorage(1);
            String userRole = getUserRole();
            String userId = getUserId();
            if (!"manage".equals(userRole)) {
                criteria.setStorageUserId(userId);
            }
            List<CkOutbillList> list = vckStockService.outbillList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    /**
     * 销售发货出库管理
     */
    @ApiOperation(value = "销售发货出库统计")
    @PostMapping("/outbillTotal")
    public CommonResult outbillTotal(@Valid CkOutbillQueryCriteria criteria) {
        try {
            List<CkOutbillList> list = vckStockService.outbillTotal(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取部门成员")
    @PostMapping("/outbillDeptUser")
    public CommonResult outbillDeptUser() {
        try {
            JSONObject jsonObject = vckStockService.outbillDeptUser();
            return CommonResult.success(jsonObject, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "oem齐套列表")
    @PostMapping("/pmcOemList")
    public CommonResult pmcOemList(PmcQueryCriteria criteria, YmPageDto ymPage) {
        try {
            if(UtilValidate.isEmpty(criteria.getBuMenName())){
                criteria.setBuMenName("oem");
            }
            TableList<ViewBaogonCailiao> list = vckStockService.pmcTableList(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "pmc分页同步")
    @RequestMapping(value = "/pmcAutoSync", method = RequestMethod.GET)
    public CommonResult autoSync(@RequestParam String buMenName) {
        try{
            if(UtilValidate.isEmpty(buMenName)){
                buMenName="oem";
            }
            pmcOrderService.autoSync(buMenName);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "列表获取缺料情况")
    @PostMapping("/getQueLiaos")
    public CommonResult getQueLiaos(@RequestBody QueLiaosQueryCriteria criteria) {
        try {
            if(UtilValidate.isEmpty(criteria.getBuMenName())){
                criteria.setBuMenName("oem");
            }
            Map<String, String> queLiaos = vckStockService.getQueLiaos(criteria);
            return CommonResult.success(queLiaos, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "列表获取齐套数量")
    @PostMapping("/getQiTaoNum")
    public CommonResult getQiTaoNum(@RequestBody QueLiaosQueryCriteria criteria) {
        try {
            if(UtilValidate.isEmpty(criteria.getBuMenName())){
                criteria.setBuMenName("oem");
            }
            Map<String, Integer> taoNum = vckStockService.getQiTaoNum(criteria);
            return CommonResult.success(taoNum, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "列表获取报工情况")
    @PostMapping("/baogongQueryList")
    public CommonResult baogongQueryList(@RequestBody QueLiaosQueryCriteria c2) {
        try {
            PmcQueryCriteria criteria=new PmcQueryCriteria();
            criteria.setBiaoShiHaoIn(c2.getBiaoShiHaoIn());
            if(UtilValidate.isEmpty(c2.getBuMenName())){
                c2.setBuMenName("oem");
            }
            criteria.setBuMenName(c2.getBuMenName());
            List<ViewBaogonCailiao> list = vckStockService.baogongQueryList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "列表获取报工情况")
    @PostMapping("/pmcWenjianList")
    public CommonResult pmcWenjianList(@RequestBody List<String> piCiHaoIn) {
        try {
            PmcQueryCriteria criteria=new PmcQueryCriteria();
            criteria.setPiCiHaoIn(piCiHaoIn);
            List<PmcWenJianGuiDto> list = vckStockService.pmcWenjianList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "齐套备注")
    @PostMapping("/pmcOemNote")
    public CommonResult pmcOemNote(@RequestParam String biaoShiHao,@RequestParam String note, String buMenName) {
        try {
            if(UtilValidate.isEmpty(buMenName)){
                buMenName="oem";
            }
            PmcNote pmcNote = pmcNoteService.getBiaoShiHaoAndBuMenName(biaoShiHao,buMenName);
            if(UtilValidate.isNotEmpty(pmcNote)){
                pmcNote.setNote(note);
            }else{
                pmcNote =new PmcNote();
                pmcNote.setId(biaoShiHao+buMenName);
                pmcNote.setBiaoShiHao(biaoShiHao);
                pmcNote.setNote(note);
                pmcNote.setBuMenName(buMenName);
            }
            pmcNoteService.saveOrUpdate(pmcNote);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "oem齐套明细")
    @PostMapping("/pmcOemItems")
    public CommonResult pmcOemItems(PmcQueryCriteria criteria) {
        try {
            List<ViewBaogonItem> list = vckStockService.pmcItemList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "oem 物料导入明细")
    @PostMapping("/oemMaterialRefList")
    public CommonResult oemMaterialRefList(PmcMaterialRefQueryCriteria criteria) {
        try {
            List<PmcMaterialRef> list1 = pmcMaterialRefService.queryAll(criteria);
            return CommonResult.success(list1, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "订单二级bom清单")
    @PostMapping("/orderBomList")
    public CommonResult orderBomList(PmcQueryCriteria criteria) {
        try {
            if(UtilValidate.isEmpty(criteria.getBuMenName())){
                criteria.setBuMenName("oem");
            }
            List<BomMaterial> list1 = vckStockService.orderBomList(criteria);
            return CommonResult.success(list1, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "oem齐套物料关联导入")
    @PostMapping("/importOemMaterialRef")
    public CommonResult importOemMaterialRef(@RequestBody ImportMaterialRefDto dto) {
        try {
            pmcMaterialRefService.importOemMaterialRef(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


}
